<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文本反转工具</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            line-height: 1.6;
        }
        .container {
            background-color: #f9f9f9;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #2c3e50;
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #34495e;
        }
        textarea {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: vertical;
            min-height: 100px;
            font-family: inherit;
            font-size: 14px;
        }
        button {
            background-color: #3498db;
            color: white;
            border: none;
            padding: 12px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #2980b9;
        }
        .result-container {
            margin-top: 30px;
            display: none;
        }
        .result-box {
            background-color: #e8f4f8;
            border-left: 4px solid #3498db;
            padding: 15px;
            border-radius: 0 4px 4px 0;
        }
        .error-container {
            margin-top: 20px;
            color: #e74c3c;
            background-color: #fdecea;
            padding: 15px;
            border-radius: 4px;
            display: none;
        }
        .loading {
            text-align: center;
            margin: 20px 0;
            display: none;
        }
        .loading-spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #3498db;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
<div class="container">
    <h1>文本反转工具</h1>

    <div class="form-group">
        <label for="inputText">输入文本：</label>
        <textarea id="inputText" placeholder="请输入要反转的文本..."></textarea>
    </div>

    <button id="processBtn">反转文本</button>

    <div class="loading" id="loading">
        <div class="loading-spinner"></div>
        <p>处理中...</p>
    </div>

    <div class="error-container" id="errorContainer"></div>

    <div class="result-container" id="resultContainer">
        <h3>反转结果：</h3>
        <div class="result-box">
            <p id="outputText"></p>
        </div>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', () => {
        const processBtn = document.getElementById('processBtn');
        const inputText = document.getElementById('inputText');
        const outputText = document.getElementById('outputText');
        const resultContainer = document.getElementById('resultContainer');
        const errorContainer = document.getElementById('errorContainer');
        const loading = document.getElementById('loading');

        processBtn.addEventListener('click', async () => {
            // 验证输入
            const text = inputText.value.trim();
            if (!text) {
                showError('请输入要反转的文本');
                return;
            }

            // 重置UI状态
            hideError();
            hideResult();
            showLoading();

            try {
                // 调用后端API
                const response = await fetch('http://localhost:8080/api/process', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
                    },
                    body: `input=${encodeURIComponent(text)}`
                });

                if (!response.ok) {
                    throw new Error(`请求失败: ${response.status} ${response.statusText}`);
                }

                const data = await response.json();

                if (data.error) {
                    throw new Error(data.error);
                }

                // 显示结果
                outputText.textContent = data.output;
                showResult();
            } catch (error) {
                showError(`处理文本时出错: ${error.message}`);
                console.error('Error:', error);
            } finally {
                hideLoading();
            }
        });

        function showLoading() {
            loading.style.display = 'block';
            processBtn.disabled = true;
        }

        function hideLoading() {
            loading.style.display = 'none';
            processBtn.disabled = false;
        }

        function showResult() {
            resultContainer.style.display = 'block';
        }

        function hideResult() {
            resultContainer.style.display = 'none';
        }

        function showError(message) {
            errorContainer.textContent = message;
            errorContainer.style.display = 'block';
        }

        function hideError() {
            errorContainer.style.display = 'none';
        }
    });
</script>
</body>
</html>